﻿@model PhoneBilling.Models.DepartmentMgtModel
@{
    ViewBag.Title = "部组管理";
    Layout = "~/Views/Shared/_Layout.cshtml"; 
}
<script type="text/javascript">
    function selectEmptyGroup() {
        $("span.file").removeClass("li-active");
        $("a.file").hide();

        $("span.folder").removeClass("li-active");
        $("span.folder#emptyGroup").addClass("li-active");
        $("a.folder").hide();

        var url = '@Url.Content("~/Manage/ExtensionMgt/")' + "null";
        var targetDiv = "#phoneList"
        $.get(url, null, function (result) {
            $(targetDiv).html(result);
        });
    }
    function selectDepartment(department) {
        $("span.file").removeClass("li-active");
        $("a.file").hide();

        $("span.folder").removeClass("li-active");
        $("span.folder#" + department).addClass("li-active");
        $("a.folder").hide();
        $("a.folder#" + department).show();
        var url = '@Url.Content("~/Manage/ExtensionMgt/")' + department;
        var targetDiv = "#phoneList"
        $.get(url, null, function (result) {
            $(targetDiv).html(result);
        });
    }
    function selectGroup(group) {
        $("span.folder").removeClass("li-active");
        $("a.folder").hide();

        $("span.file").removeClass("li-active");
        $("span.file#" + group).addClass("li-active");
        $("a.file").hide();
        $("a.file#" + group).show();
        var url = '@Url.Content("~/Manage/ExtensionMgt/")' + group;
        var targetDiv = "#phoneList"
        $.get(url, null, function (result) {
            $(targetDiv).html(result);
        });
    }

    function deptDel(id, name) {
        var r = confirm("确认删除部门：" + name + "吗？");
        if (r == true) {
            window.location.href = "/Manage/DepartmentDelete?id=" + id;
        }
        return false;
    }
    function groupDel(id, name) {
        var r = confirm("确认删除分组：" + name + "吗？");
        if (r == true) {
            window.location.href = "/Manage/GroupDelete?id=" + id;
        }
        return false;
    }
</script>
@{
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    string text = "";
    for (int i = 0; i < Model.DepartmentList.Count; i++)
    {
        var department = Model.DepartmentList[i];
        if (i == 0)
        {
            sb.Append(string.Format("{{ text:'{0}', uid:'{1}',utype:'Department',ulevel:'0' ", department.名称.Trim(), department.ID));
        }
        else
        {
            sb.Append(string.Format(",{{ text:'{0}', uid:'{1}',utype:'Department',ulevel:'0' ", department.名称.Trim(), department.ID));
        }

        if (department.分组List.Count > 0)
        {
            sb.Append(",children: [");

            for (int j = 0; j < department.分组List.Count; j++)
            {
                var group1 = department.分组List[j];
                if (j == 0)
                {
                    sb.Append(string.Format("{{ text:'{0}', uid:'{1}',utype:'Group',ulevel:'1' ", group1.名称.Trim(), group1.ID));
                }
                else
                {
                    sb.Append(string.Format(",{{ text:'{0}', uid:'{1}',utype:'Group',ulevel:'1' ", group1.名称.Trim(), group1.ID));
                }

                if (group1.分组List.Count > 0)
                {
                    sb.Append(",children: [");

                    for (int k = 0; k < group1.分组List.Count; k++)
                    {
                        var group2 = group1.分组List[k];
                        if (k == 0)
                        {
                            sb.Append(string.Format("{{ text:'{0}', uid:'{1}',utype:'Group',ulevel:'2' ", group2.名称.Trim(), group2.ID));
                        }
                        else
                        {
                            sb.Append(string.Format(",{{ text:'{0}', uid:'{1}',utype:'Group',ulevel:'2' ", group2.名称.Trim(), group2.ID));
                        }

                        if (group2.分组List.Count > 0)
                        {
                            sb.Append(",children: [");

                            for (int l = 0; l < group2.分组List.Count; l++)
                            {
                                var group3 = group2.分组List[l];
                                if (l == 0)
                                {
                                    sb.Append(string.Format("{{ text:'{0}', uid:'{1}',utype:'Group',ulevel:'3' ", group3.名称.Trim(), group3.ID));
                                }
                                else
                                {
                                    sb.Append(string.Format(",{{ text:'{0}', uid:'{1}',utype:'Group',ulevel:'3' ", group3.名称.Trim(), group3.ID));
                                }

                                sb.AppendLine("}");

                            }

                            sb.Append("]");
                        }

                        sb.AppendLine("}");

                    }

                    sb.Append("]");
                }

                sb.AppendLine("}");

            }

            sb.Append("]");
        }

        sb.AppendLine("}");

    }
    sb.Append(string.Format(",{{ text:'{0}', uid:'{1}',utype:'empty'}} ", "（未分组分机）", ""));

    string data = sb.ToString();

    if (Model.GroupID != null)
    {
        System.Text.StringBuilder loadSB = new System.Text.StringBuilder();
        loadSB.AppendLine(string.Format("var url = '/Manage/ExtensionMgt/{0}'",Model.GroupID));
        loadSB.AppendLine("var targetDiv = '#phoneList';");
        loadSB.AppendLine("$.get(url, null, function (result) {$(targetDiv).html(result);});");

        text = loadSB.ToString();             
                 
    }
}
@if (Model.Message != null)
{<h2>@Model.Message</h2>}
<script type="text/javascript">
    $(document).ready(function () {
        var menu = $.ligerMenu({ top: 100, left: 100, width: 120, items:
            [
            { text: '新增部门', click: newdeptclick },
            { line: true },
            { text: '部门排序', click: deptsortclick },
            { line: true },
            { text: '新增子分组', click: newclick },
            { line: true },
            { text: '修改', click: modifyclick },
            { line: true },
            { text: '排序', click: sortclick },
            { line: true },
            { text: '迁移', click: motionclick },
            ]
        });
        var isStart = false;
        var tree = $("#tree").ligerTree({ checkbox: false, parentIcon: null, childIcon: null,
        onContextmenu: function (node, e) {
            actionNodeID = node;
            menu.show({ top: e.pageY, left: e.pageX });
            return false;
        },
            data: [@Html.Raw(data)]
        ,onClick: function (node) {
            if(isStart == false)
                return false;
            if(node.data.utype == "empty")
            {
                 var url = '@Url.Content("~/Manage/ExtensionMgt/")' + "null";
                 var targetDiv = "#phoneList";
                 $.get(url, null, function (result) {
                     $(targetDiv).html(result);
                 });
            }
            else if(node.data.utype == "Department")
            {
                var url = '@Url.Content("~/Manage/ExtensionMgt/")' + node.data.uid;
                var targetDiv = "#phoneList";
                $.get(url, null, function (result) {
                    $(targetDiv).html(result);
                });
            }
            else if(node.data.utype == "Group")
            {
                var url = '@Url.Content("~/Manage/ExtensionMgt/")' + node.data.uid;
                var targetDiv = "#phoneList";
                $.get(url, null, function (result) {
                    $(targetDiv).html(result);
                });
            }
        }
        });
        tree.collapseAll();
        isStart = true;

        @Html.Raw(text)

    })
    function newdeptclick(item, i) {
        window.location.href = "/Manage/DepartmentEdit";
    }
    function deptsortclick(item, i) {
        window.location.href = "/Manage/DepartmentSort";
    }
    function newclick(item, i) {
        if(actionNodeID.data.ulevel == '3')
        {
            alert("最多只能3级子组织。");
            return;
        }
        var path = "/Manage/GroupEdit?parentID="+actionNodeID.data.uid+"&parentType="+actionNodeID.data.utype;
        window.location.href=path;
    }
    function modifyclick(item, i) {
        var path = "/Manage/GroupEdit?id="+actionNodeID.data.uid;
        window.location.href=path;
    }
    function sortclick(item, i) {
        if(actionNodeID.data.ulevel == '3')
        {
            alert("3级子组织不能排序。");
            return;
        }
        var path = "/Manage/GroupSort?id="+actionNodeID.data.uid+"&type="+actionNodeID.data.utype;;
        window.location.href=path;
    }
    function motionclick(item, i) {
        if(actionNodeID.data.ulevel == '0')
        {
            alert("不能直接迁移部门。");
            return;
        }

        var path = "/Manage/GroupMotion?id="+actionNodeID.data.uid;
        window.location.href=path;  
    }
    
</script>
@*<div class="system_toolbar">
    <input type="button" id="btnNewDepartment" value="新增部门" />
    <input type="button" id="btnSortDepartment" value="排序" />
    <div class="system_toolbar_label">
        @Html.LabelFor(m => m.ExtCounts)
        @Html.DisplayFor(m => m.ExtCounts)
    </div>
</div>*@
<div class="department_mgt">
<div class="system_contain_group col25">
    @*<div id="btnNewDepartment"/>
    <div id="btnSortDepartment"/>*@ @*<p id="btn-create" class="box"><a href="/Manage/DepartmentEdit"><span>新增部门</span></a><span>   </span><a href="/Manage/DepartmentSort"><span>排序</span></a></p>*@
    <h3 class="title">部组</h3>
    <div class="system_scroll_container">
    <ul id="tree" />
    </div>
    @*<ul id="tree">
     @{foreach (var department in Model.DepartmentList)
     {
        <li><span class="folder" id="@department.ID" onclick="selectDepartment('@department.ID')">@department.名称</span> 
        <span>
        @Html.ActionLink("新增子分组", "GroupEdit", new { parentID = department.ID, parentType = "Department" }, new { @class = "folder" , id=department.ID })
        @Html.ActionLink("修改", "DepartmentEdit", new { id = department.ID }, new { @class = "folder", id = department.ID })
        <a href='javascript:' onclick="deptDel('@department.ID','@department.名称')" class ="folder" id='@department.ID'>删除</a>
        @Html.ActionLink("排序", "GroupSort", new { id = department.ID,type="Department" }, new { @class = "folder", id = department.ID })
        </span>
                @if(department.分组List.Count >0)
        {
        <ul>
            @{foreach (var group1 in department.分组List)
              {
                <li><span id="@group1.ID" onclick="selectGroup('@group1.ID')">@group1.名称</span> 
                @Html.ActionLink("新增子分组", "GroupEdit", new { parentID = group1.ID, parentType = "Group" }, new { @class = "file", id = group1.ID })
                @Html.ActionLink("修改", "GroupEdit", new { id = group1.ID }, new { @class = "file", id = group1.ID })
                <a href='javascript:' onclick="groupDel('@group1.ID','@group1.名称')" class = "file" id='@group1.ID'>删除</a>
                @Html.ActionLink("排序", "GroupSort", new { id = group1.ID, type = "Group" }, new { @class = "file", id = group1.ID })
                @Html.ActionLink("迁移", "GroupMotion", new { id = group1.ID }, new { @class = "file", id = group1.ID })
            
                @if (group1.分组List.Count > 0)
                {
                <ul>
                    @{foreach (var group2 in group1.分组List)
                      {
                        <li><span class="file" id="@group2.ID" onclick="selectGroup('@group2.ID')">@group2.名称</span> 
                        @Html.ActionLink("新增子分组", "GroupEdit", new { parentID = group2.ID, parentType = "Group" }, new { @class = "file", id = group2.ID })
                        @Html.ActionLink("修改", "GroupEdit", new { id = group2.ID }, new { @class = "file", id = group2.ID })
                        <a href='javascript:' onclick="groupDel('@group2.ID','@group2.名称')" class = "file" id='@group2.ID'>删除</a>
                        @Html.ActionLink("排序", "GroupSort", new { id = group2.ID, type = "Group" }, new { @class = "file", id = group2.ID })
                        @Html.ActionLink("迁移", "GroupMotion", new { id = group2.ID }, new { @class = "file", id = group2.ID })
                
                        @if (group2.分组List.Count > 0)
                        {
                        <ul>
                            @{foreach (var group3 in group2.分组List)
                              {
                                <li><span id="@group3.ID" onclick="selectGroup('@group3.ID')">@group3.名称</span> 
                                @Html.ActionLink("修改", "GroupEdit", new { id = group3.ID }, new { @class = "file", id = group3.ID })
                                <a href='javascript:' onclick="groupDel('@group3.ID','@group3.名称')" class = "file" id='@group3.ID'>删除</a>
                                @Html.ActionLink("迁移", "GroupMotion", new { id = group3.ID }, new { @class = "file", id = group3.ID })
                                </li>
                              }}
                        </ul>
                        }
                
                        </li>
                      }}
                </ul>
                }
                </li>
              }}
        </ul>
        }
        
        </li>
     }}
     <li><span class="folder" id="emptyGroup" onclick="selectEmptyGroup()">(未分组分机)</span></li>
     </ul>*@
</div>
<div class="system_contain_group col75">
    <div id="phoneList">
    </div>
</div>
</div>